import authApi from "@/api/auth";
import menuApi from "@/api/system/menu";
import router from "@/router";
import {useGlobalStoreHook} from "@/store/global";
import storage from "@/utils/storage";
import modal from "@/utils/modal";
import tool from "@/utils/tool";

const token = {

    login: (data, callback) => {
        authApi.login.post(data).then(response => {
            if (response.status) {
                storage.cookie.set("APP_USER_TOKEN", response.data.token);
                storage.data.set("APP_USER_INFO", response.data.userInfo);
                storage.data.set("APP_TENANT_INFO", response.data.tenantInfo);
            }
            if (tool.isFunction(callback)) {
                callback(response);
            }
        });
    },

    logout: () => {
        modal.openLoading("正在退出系统，请稍后...");
        if (token.getUserToken()) {
            authApi.logout.get().then(response => {
                if (response.status) {
                    // 只是清除登录信息
                    storage.cookie.remove("APP_USER_TOKEN");
                    router.replace({path: "/login"}).then(() => {
                        modal.closeLoading();
                    });
                } else {
                    modal.closeLoading();
                }
            });
        } else {
            // 只是清除登录信息
            storage.cookie.remove("APP_USER_TOKEN");
            router.replace({path: "/login"}).then(() => {
                modal.closeLoading();
            });
        }
    },

    clearCache: () => {
        modal.openLoading("正在清理缓存，请稍后...");
        authApi.logout.get().then(response => {
            if (response.status) {
                // 清除所有缓存
                storage.session.clear();
                storage.data.clear();
                storage.cookie.remove("APP_USER_TOKEN");
                router.replace({path: "/login"}).then(() => {
                    modal.closeLoading();
                    window.location.reload();
                });
            } else {
                modal.warningMessage(response.message);
                modal.closeLoading();
            }
        });
    },

    getUserToken: () => {
        return storage.cookie.get("APP_USER_TOKEN");
    },

    getUserInfo: () => {
        return storage.data.get("APP_USER_INFO");
    },

    getUserId: () => {
        return storage.data.get("APP_USER_INFO")?.userId;
    },

    getUserCode: () => {
        return storage.data.get("APP_USER_INFO")?.userCode;
    },

    getNickName: () => {
        return storage.data.get("APP_USER_INFO")?.nickName;
    },

    getLoginName: () => {
        return storage.data.get("APP_USER_INFO")?.loginName;
    },

    getDeptId: () => {
        return storage.data.get("APP_USER_INFO")?.deptId;
    },

    getDeptName: () => {
        return storage.data.get("APP_USER_INFO")?.deptName;
    },

    getTenantInfo: () => {
        return storage.data.get("APP_TENANT_INFO");
    },

    isSuperTenant: () => {
        return storage.data.get("APP_TENANT_INFO")?.isPlatform || storage.data.get("APP_TENANT_INFO")?.code === 'TEN_SUPER_TENANT';
    },

    getTenantSource: () => {
        return storage.data.get("APP_TENANT_INFO")?.tenantSource;
    },

    getAuthCodeList: () => {
        const userInfo = storage.data.get("APP_USER_INFO");
        return userInfo?.authCodeList;
    },

    getRoleCodeList: () => {
        const userInfo = storage.data.get("APP_USER_INFO");
        return userInfo?.roleCodeList;
    },

    getPowerCodeList: () => {
        const userInfo = storage.data.get("APP_USER_INFO");
        return userInfo?.powerCodeList;
    },

    getPostCodeList: () => {
        const userInfo = storage.data.get("APP_USER_INFO");
        return userInfo?.postCodeList;
    },

    loadDynamicsMenu: (callback) => {
        menuApi.tree.get({enabled: true}).then(response => {
            if (response.status) {
                const menuList = response.data ? response.data : [];
                useGlobalStoreHook().setMenuList(menuList);
            } else {
                modal.warningMessage(response.message);
            }
            if (tool.isFunction(callback)) {
                callback(response);
            }
        });
    },

}

export default token;